package com.wxd.framework.common;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wxd.framework.common.data.TypeCaseHelper;

public class Criteria {
	/**
	 * 存放条件查询值
	 */
	private Map<String, Object> condition;	
	/**
	 * 是否相异
	 */
	protected boolean distinct;
	/**
	 * 排序字段
	 */
	protected String orderByClause;
	
	//传值时使用
	private Integer pageNo;
	private Integer pageSize;

	//查询时使用
	private Integer startIndex;
	private Integer endSize;
	
	public Criteria() {
		condition = new HashMap<String, Object>();
	}
	
	protected Criteria(Criteria example) {
		this.orderByClause = example.orderByClause;
		this.condition = example.condition;
		this.distinct = example.distinct;
		this.startIndex = example.startIndex;
		this.endSize = example.endSize;
	}
	
	public void clear(){
		this.condition.clear();
		this.orderByClause = null;
		this.distinct = false;
		this.startIndex = null;
		this.endSize = null;
	}
	
	/**
	 * @param condition
	 *            查询的条件名称
	 * @param value
	 *            查询的值
	 */
	public Criteria put(String condition, Object value) {
		this.condition.put(condition, value);
		return (Criteria) this;
	}
	
	/**
	 * 得到键值，C层和S层的参数传递时取值所用<br>
	 * 自行转换对象
	 * 
	 * @param key
	 *            键值
	 * @return 返回指定键所映射的值
	 */
	public Object get(String key) {
		return this.condition.get(key);
	}
		
	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}

	public Map<String, Object> getCondition() {
		return condition;
	}
	
	/**
	 * @param orderByClause
	 *            排序字段
	 */
	public void setOrderByClause(String orderByClause) {
		this.orderByClause = orderByClause;
	}

	/**
	 * @param distinct
	 *            是否相异
	 */
	public void setDistinct(boolean distinct) {
		this.distinct = distinct;
	}
	
	/**
	 * @param startIndex
	 *            开始记录数
	 */
	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	/**
	 * @param endSize
	 *            结束记录数
	 */
	public void setEndSize(Integer endSize) {
		this.endSize = endSize;
	}
	
	/**
	 * 以BigDecimal类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return BigDecimal 键值
	 */
	public BigDecimal getAsBigDecimal(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "BigDecimal", null);
		if (obj != null)
			return (BigDecimal) obj;
		else
			return null;
	}

	/**
	 * 以Date类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return Date 键值
	 */
	public Date getAsDate(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "Date", "yyyy-MM-dd");
		if (obj != null)
			return (Date) obj;
		else
			return null;
	}

	/**
	 * 以Integer类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return Integer 键值
	 */
	public Integer getAsInteger(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "Integer", null);
		if (obj != null)
			return (Integer) obj;
		else
			return null;
	}

	/**
	 * 以Long类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return Long 键值
	 */
	public Long getAsLong(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "Long", null);
		if (obj != null)
			return (Long) obj;
		else
			return null;
	}

	/**
	 * 以String类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return String 键值
	 */
	public String getAsString(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "String", null);
		if (obj != null)
			return (String) obj;
		else
			return "";
	}

	/**
	 * 以Timestamp类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return Timestamp 键值
	 */
	public Timestamp getAsTimestamp(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "Timestamp", "yyyy-MM-dd HH:mm:ss");
		if (obj != null)
			return (Timestamp) obj;
		else
			return null;
	}

	/**
	 * 以Boolean类型返回键值
	 * 
	 * @param key
	 *            键名
	 * @return Timestamp 键值
	 */
	public Boolean getAsBoolean(String key) {
		Object obj = TypeCaseHelper.convert(get(key), "Boolean", null);
		if (obj != null)
			return (Boolean) obj;
		else
			return null;
	}
	
	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	/**
	 * 添加查询条件
	 * @param strings
	 */
	public void setCondition(String...strings){
		for(String str : strings){
			String[] arr = str.split(FrameConstants.NORMAL_FLAG, -1);
			String key = arr[0];
			String value = "null".equals(arr[1]) ? "" : arr[1];
			Object val;
			if ("true".equals(value)) {
				val = true;
			}else if ("false".equals(value)) {
				val = false;
			}else {
				val = value;
			}						
			if (StringUtils.isNotBlank(key)) {
				if (key.startsWith(FrameConstants.COMBBOX_CONDITION_PREFIX)) {//若为combbox
					if (StringUtils.isNotBlank(value) && !FrameConstants.COMBBOX_TEMP_VALUE.equals(value)) {
						this.put(key, val);
					}
				}else {
					if (StringUtils.isNotBlank(value)) {
						this.put(key, val);
					}
				}
			}
		}
	}
}
